#include "../mytcp.h"

#define TCP_SERVER_IP "192.168.31.13"
#define TCP_SERVER_PORT 55555

int main(int argc, char const *argv[])
{
    int Ret;
    Ret = TcpServerInit( TCP_SERVER_IP, TCP_SERVER_PORT );
    if( -1 == Ret )
    {
        perror( "fail TcpServerInit" );
        return -1;
    }

    int ConnectSockFd = 0;
    char ClintIp[100] = {0};
    int ClintPort = 0;
    Ret = TcpServerAccept( &ConnectSockFd, ClintIp, &ClintPort );
    if ( -1 == Ret )
    {
        perror( "fail TcpServerAccept" );
        return -1;
    }
    printf( "Success accept TCP Clint %s:%d\n", ClintIp, ClintPort );

    ssize_t SizeRecv;
    char RecvBuff[100] = {0};
    char SendBuff[100] = "Echo from TCP Server";
    while ( (SizeRecv = recv( ConnectSockFd, RecvBuff, sizeof(RecvBuff), 0 )) > 0 )
    {
        printf( "Recv from TCP Clint: %s\n", RecvBuff );
        
        ssize_t SizeSend;
        SizeSend = send( ConnectSockFd, SendBuff, strlen(SendBuff)+1, 0 );
        if ( -1 == SizeSend )
        {
            perror( "fail send" );
            return -1;
        } 
    }

    if ( -1 == SizeRecv )
    {
        perror( "fail recv" );
        return -1;
    }

    close( ConnectSockFd );
    TcpServerClose();
    
    return 0;
}
